import { defineStore } from 'pinia'
import { login } from '@/api/login'

export const useUserStore = defineStore('user', {
  state: () => ({
    token: '',
    userInfo: null
  }),
  
  actions: {
    async login(userInfo) {
      try {
        const { data } = await login(userInfo)
        this.token = data.token
        this.userInfo = data.userInfo
        return data
      } catch (error) {
        throw new Error(error.message || '登录失败')
      }
    },
    
    logout() {
      this.token = ''
      this.userInfo = null
    }
  },
  
  persist: true
})
